package com.gsit.expenditure.report;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

import com.gsit.expenditure.report.domain.Bill;
import com.gsit.expenditure.report.domain.Client;
import com.gsit.expenditure.report.ds.InvoiceDataSourceFactory;
import com.gsit.sample.converter.api.AmountByWord;


public class MainApp {
	
	final static String SOURCE_PATH = "/reports/InvoiceTest.jrxml";
	final static String DEST_PATH = "/reports";
	final static String DEST_FILE_NAME = "M-K-Mehta.pdf";
	


	public static void main(String a[]) {
		
		try {
			
			StringBuffer destPath = new StringBuffer(System.getProperty("user.home"));
			destPath.append("/");
			destPath.append("ExpenditureApp");
			destPath.append(DEST_PATH);
			
			File file = new File(destPath.toString());
			if(!file.exists()){
				if(!file.mkdirs()){
					throw new Exception("Can not create file");
				}
			}
			destPath.append("/");
			destPath.append(DEST_FILE_NAME);
			
			MainApp mainApp = new MainApp();
			
			
			Set<Bill> bills = new HashSet<Bill>(); 
			for (int i = 0; i < 10; i++) {
				Bill bill = new Bill();
				bill.setReferenceNumber(""+(i+1));
				bill.setCreatedDate(bill.getCreatedDate());
				/*	bill.setVehicle(vehicle);
				bill.set*/
				bills.add(bill);
			}
			mainApp.createJasperReportForInvoice(SOURCE_PATH, destPath.toString(),bills);
			
			System.out.println(">>>>>>>> END >>>>>>>>>>>>>");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	 public  void createJasperReportForInvoice(String sourcePath, String destPath, Collection<Bill> bills)throws Exception{
		 
		Map<String, Object> parameters = new HashMap<String, Object>();
		
		parameters.put("bills", bills);
		
		Client client = new Client();
		Double totalAmount = 0.0;
		for (Bill bill : bills) {
			System.out.println(" bill ::::::: "+ bill);
			
			if (bill.getAmount() != null) {
				totalAmount = totalAmount + bill.getAmount();

				System.out.println("Total Amount*********"+totalAmount);

			}

			
			if(StringUtils.isNotBlank(bill.getCustomerName()) && bill.getCustomerName() != null){
			 client.setCustomerName(bill.getCustomerName());
			 client.setCity(bill.getCity());
			 client.setState(bill.getState());
			 client.setVehicleName(bill.getVehicleNumber());
			}
		}
		
		AmountByWord amountByWord = new AmountByWord();
		amountByWord.setLanguage( "en" );
		amountByWord.setCurrency( "in" );
		String amountString = amountByWord.getAmountByWord(totalAmount);

		System.out.println("TOTAL AMOUNT******************"+amountString);

		
		 String path = MainApp.class.getResource("/image/logo.png").getPath().toString();
		 System.out.println(path);
		 
		 client.setImagePath(path);
		 
		 System.out.println( "customer aname " +bills.iterator().next().getCustomerName() );
		 
		 /*client.setCustomerName(bills.iterator().next().getCustomerName());
		 client.setCity(bills.iterator().next().getCity());
		 client.setState(bills.iterator().next().getState());
		 client.setVehicleName(bills.iterator().next().getVehicleNumber());*/
		 
		 parameters.put("client", client);
		 parameters.put("total", totalAmount);
		 parameters.put("amountString", amountString);
		 
		 System.out.println("Map Parameters>>>>>>>>>>>>>>>>"+parameters);
		 try {
				JasperReport jasperReport = JasperCompileManager.compileReport(MainApp.class.getResourceAsStream(sourcePath));
				JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,InvoiceDataSourceFactory.createBillDS(bills));
				JasperExportManager.exportReportToPdfFile(jasperPrint, destPath);
				
		} catch (Exception e) {
				e.printStackTrace();
		}
	 }
	
	 
	
	
	 
	 

}
